package com.proactive.webapp.controller;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.amazonaws.services.s3.model.S3Object;
import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.DesignDAO;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.util.AwsUtil;

@Controller
public class DownloadController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value="/download", method=RequestMethod.GET)
    public ModelAndView download(int id, String t, HttpSession session, HttpServletRequest request,
    		HttpServletResponse response) throws Exception {
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		String loginAccount = loginMember==null?"":loginMember.getAccount();
		AwsUtil awsUtil = new AwsUtil();
		awsUtil.setPropertyPath(request.getServletContext().getRealPath("../AwsCredentials.properties"));
		awsUtil.getInstance();
		if(t.equals("1")){
			CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
			Course course = new Course();
			course.setId(id);
			course = courseDAO.get(course);
			if((course.getStatus() == 1) || (loginAccount.equals(course.getAccount()) && course.getStatus()==2)){
				
				S3Object object = awsUtil.getS3Object("proactive", course.getFilePath());
				InputStream reader = new BufferedInputStream(object.getObjectContent());
				
				int length = 0;
				ServletOutputStream op = response.getOutputStream();
				ServletContext context = request.getServletContext();
				String mimetype = context.getMimeType(course.getFilePath());
				response.setContentType((mimetype != null) ? mimetype
						: "application/octet-stream");
				response.setContentLength((int) object.getObjectMetadata().getContentLength());
				response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(course.getFileName().getBytes("UTF-8"),"ISO-8859-1") + "\"");

				byte[] bbuf = new byte[200];
				DataInputStream in = new DataInputStream(reader);

				while ((in != null) && ((length = in.read(bbuf)) != -1)) {
					op.write(bbuf, 0, length);
				}
				in.close();
				op.flush();
				op.close();
				
				model.setViewName("redirect:"+request.getHeader("referer"));
				
				/*
				String filename = request.getServletContext().getRealPath(course.getFilePath());

				File f = new File(filename);
				int length = 0;
				ServletOutputStream op = response.getOutputStream();
				ServletContext context = request.getServletContext();
				String mimetype = context.getMimeType(filename);
				response.setContentType((mimetype != null) ? mimetype
						: "application/octet-stream");
				response.setContentLength((int) f.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(course.getFileName().getBytes("UTF-8"),"ISO-8859-1") + "\"");

				byte[] bbuf = new byte[200];
				DataInputStream in = new DataInputStream(new FileInputStream(f));

				while ((in != null) && ((length = in.read(bbuf)) != -1)) {
					op.write(bbuf, 0, length);
				}
				in.close();
				op.flush();
				op.close();
				*/
				
			}
			else {
				model.setViewName("redirect:/course");
			}
		}
		else if(t.equals("2")){
			DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
			Design design = new Design();
			design.setId(id);
			design = designDAO.get(design);
			if((design.getStatus() == 1) || (loginAccount.equals(design.getAccount()) && design.getStatus()==2)){
				S3Object object = awsUtil.getS3Object("proactive", design.getFilePath());
				InputStream reader = new BufferedInputStream(object.getObjectContent());
				
				int length = 0;
				ServletOutputStream op = response.getOutputStream();
				ServletContext context = request.getServletContext();
				String mimetype = context.getMimeType(design.getFilePath());
				response.setContentType((mimetype != null) ? mimetype
						: "application/octet-stream");
				response.setContentLength((int) object.getObjectMetadata().getContentLength());
				response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(design.getFileName().getBytes("UTF-8"),"ISO-8859-1") + "\"");

				byte[] bbuf = new byte[200];
				DataInputStream in = new DataInputStream(reader);

				while ((in != null) && ((length = in.read(bbuf)) != -1)) {
					op.write(bbuf, 0, length);
				}
				in.close();
				op.flush();
				op.close();
				
				model.setViewName("redirect:"+request.getHeader("referer"));
				
				/*
				String filename = request.getServletContext().getRealPath(design.getFilePath());

				File f = new File(filename);
				int length = 0;
				ServletOutputStream op = response.getOutputStream();
				ServletContext context = request.getServletContext();
				String mimetype = context.getMimeType(filename);
				response.setContentType((mimetype != null) ? mimetype
						: "application/octet-stream");
				response.setContentLength((int) f.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(design.getFileName().getBytes("UTF-8"),"ISO-8859-1") + "\"");

				byte[] bbuf = new byte[200];
				DataInputStream in = new DataInputStream(new FileInputStream(f));

				while ((in != null) && ((length = in.read(bbuf)) != -1)) {
					op.write(bbuf, 0, length);
				}
				in.close();
				op.flush();
				op.close();
				*/
				
				
			}
			else
				model.setViewName("redirect:/design");
			
		}
		
		return model;
		
    }
}
